var SEC_PER_FRAME = 1 / 60;

var timer = new FrameTimer();
var game_time = 0;
var frame_time = 0
var head_mouse = null;
var score = 0;
var dead_mice = 0;
var next_level = 10;
var last_mouse_time = 0;
var SEC_PER_MOUSE_EMISSION = 5;
var MAX_MICE = 100;

function drawScoreboard() {
  var canvas = document.getElementById("scoreboard");
  var c2d = canvas.getContext("2d");
  c2d.clearRect(0, 0, canvas.width, canvas.height);
  drawSprite(c2d, 'white_mouse', 0, 0);
  drawNumber(c2d, score, 3, 32, 0);
  drawSprite(c2d, 'slash', 4 * 32, 0);
  drawNumber(c2d, next_level, 3, 5 * 32, 0);
  drawSprite(c2d, 'skull', 9 * 32, 0);
  drawNumber(c2d, dead_mice, 3, 10 * 32, 0);
}

function mapMouseMove(e) {
  var canvas = document.getElementById("canvas");
  hover_x = Math.floor((e.pageX - canvas.offsetLeft - 4) / tile_width);
  hover_y = Math.floor((e.pageY - canvas.offsetTop - 4) / tile_height);
}

function mapMouseOut() {
  hover_x = hover_y = -1;
}

function mapMouseClick() {
  if (hover_x >= 0 && hover_y >= 0) {
    if (map[hover_y * map_width + hover_x] == ' ' &&
        inventory[inventory_selected].count > 0) {
      setTile(hover_x, hover_y, inventory[inventory_selected].tile);
      --inventory[inventory_selected].count;
      hover_x = hover_y = -1;
      drawButton(inventory_selected);
    } else {
      var map_tile = map[hover_y * map_width + hover_x];
      for (var i = 0; i < inventory.length; ++i) {
        if (inventory[i].tile == map_tile) {
          setTile(hover_x, hover_y, ' ');
          if (inventory[i].count < 9) {
            ++inventory[i].count;
          }
          drawButton(i);
          break;
        }
      }
    }
  }
}

function frame() {
  timer.tick();
  frame_time += timer.getSeconds();
  while (game_time < frame_time) {
    // TODO: update mice
    game_time += SEC_PER_FRAME;
  }
  drawTiles();

  if (game_time > (last_mouse_time + SEC_PER_MOUSE_EMISSION) ) {
      emitMice();
      last_mouse_time += SEC_PER_MOUSE_EMISSION;
  }

  var current_mouse = head_mouse;
  while (current_mouse) {
      updateMouseState(current_mouse);
      drawMouse(current_mouse);
      current_mouse = current_mouse.next;
  }
}

function teleporterOutExists() {
    for (y = 0; y < map_height; ++y) {
        for (x = 0; x < map_width; ++x) {
            if (getTileTypeAtCoords(x, y) == 'T') {
                return true;
            }
        }
    }
    return false;
}


function emitMice() {
    for (y = 0; y < map_height; ++y) {
        for (x = 0; x < map_width; ++x) {
            if (getTileTypeAtCoords(x, y) == 's') {
                emitMouse(x, y);
            }
        }
    }
}

function numMice() {
    var current_mouse = head_mouse;
    var count = 0;

    while (current_mouse != null) {
        current_mouse = current_mouse.next;
        count += 1;
    }

    return count;
}

function addMouse(mouse) {
    var current_mouse = head_mouse;

    if (current_mouse == null) {
        head_mouse = mouse;
    }

    else {
        while (current_mouse.next != null) {
            current_mouse = current_mouse.next;
        }
        current_mouse.next = mouse;
    }
}

function emitMouse(x, y) {
    if (numMice() > MAX_MICE) {
        return;
    }
    // find the closest open space
    var mouse = null;
    if (!outOfBounds(x, y-1) && tiles[getTileTypeAtCoords(x, y-1)].passable) {
        // create mouse 1 space north, heading north;
        mouse = new Mouse();
        mouse.x = mouse.last_x = x;
        mouse.y = mouse.last_y = y-1;
        mouse.heading = HEADING_NORTH;
    } else if (!outOfBounds(x+1, y) && tiles[getTileTypeAtCoords(x+1, y)].passable) {
        // create mouse 1 space east, heading east;
        mouse = new Mouse();
        mouse.x = mouse.last_x = x+1;
        mouse.y = mouse.last_y = y;
        mouse.heading = HEADING_EAST;
    } else if (!outOfBounds(x, y+1) && tiles[getTileTypeAtCoords(x, y+1)].passable) {
        // create mouse 1 space south, heading south;
        mouse = new Mouse();
        mouse.x = mouse.last_x = x;
        mouse.y = mouse.last_y = y+1;
        mouse.heading = HEADING_SOUTH;
    } else if (!outOfBounds(x-1, y) && tiles[getTileTypeAtCoords(x-1, y)].passable) {
        // create mouse 1 space west, heading west;
        mouse = new Mouse();
        mouse.x = mouse.last_x = x-1;
        mouse.y = mouse.last_y = y;
        mouse.heading = HEADING_WEST;
    } else {
        // do nothing?
    }

    if (mouse) {
        addMouse(mouse);
    }
}

function init() {
  timer.tick();
  drawButtons();
  drawScoreboard();

  //  head_mouse = new Mouse();
  //head_mouse.x = 7;
  // head_mouse.y = 8;
  //head_mouse.sprite_x = 7;
  //head_mouse.sprite_y = 8;

  setInterval(frame, 1000 * SEC_PER_FRAME);
}
